const db = require("../../db/index");

exports.create = (req, res) => {
  const termInfo = req.body;
  if (!termInfo.cell_id || !termInfo.dev_eui) {
    return res.send({
      status: 1,
      message: "生产单元的id或终端deveui不能为空",
    });
  }
  let sql_query = "select * from terminal_list where cell_id=? and deveui=?";
  db.query(sql_query, [termInfo.cell_id, termInfo.dev_eui], (err, results) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    if (results.length > 0) {
      return res.send({
        status: 1,
        message: "终端已存在",
      });
    }
    let sql_insert = "insert into terminal_list set ?";
    db.query(
      sql_insert,
      {
        cell_id: termInfo.cell_id,
        deveui: termInfo.dev_eui,
      },
      (err, results) => {
        if (err) {
          return res.send({
            status: 2,
            message: err.message,
          });
        }
        if (results.affectedRows !== 1) {
          return res.send({
            status: 1,
            message: "生产单元添加失败，请稍后再试！",
          });
        }
        res.send({ status: 0, message: "添加成功！" });
      }
    );
  });
};
exports.deleteByTermId = (req, res) => {
  const termId = req.query.term_id;
  if (!termId) {
    return res.send({
      status: 1,
      message: "终端id缺失",
    });
  }
  let sql_query = "select * from terminal_list where id = ?";
  db.query(sql_query, termId, (err, result) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    if (result.length == 0) {
      return res.send({
        status: 1,
        message: "删除单元不存在",
      });
    }

    let sql_delete = "delete from terminal_list where id = ?";
    db.query(sql_delete, termId, (err, result) => {
      if (err) {
        return res.send({
          status: 2,
          message: err.message,
        });
      }
      if (result.affectedRows != 1) {
        return res.send({
          status: 1,
          message: "删除失败，请稍后重试!",
        });
      }
      return res.send({
        status: 0,
        message: "删除成功",
      });
    });
  });
};
exports.updateByTermId = (req, res) => {
  const termInfo = req.body;

  if (!termInfo.cell_type && !termInfo.term_id) {
    return res.send({
      status: 1,
      message: "生产单元种类和终端ID同时缺失",
    });
  }
  let sql_query = "select id from cell_list where type = ?";
  db.query(sql_query, termInfo.cell_type, (err, result) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    if (result.length == 0) {
      return res.send({
        status: 1,
        message: "修改单元不存在",
      });
    }

    let sql_update = "update terminal_list set ? where id = ?";
    db.query(
      sql_update,
      [
        {
          cell_id: result[0].id,
          deveui: termInfo.deveui,
        },
        termInfo.term_id,
      ],
      (err, result) => {
        if (err) {
          return res.send({
            status: 2,
            message: err.message,
          });
        }
        if (result.affectedRows != 1) {
          return res.send({
            status: 1,
            message: "修改失败，请稍后重试!",
          });
        }
        return res.send({
          status: 0,
          message: "修改成功",
        });
      }
    );
  });
};

exports.getAll = (req, res) => {
  const reqInfo = req.query;
  let sql_query = `select * from terminal_list limit ${reqInfo.offset},${reqInfo.limit}`;
  db.query(sql_query, (err, result) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    return res.send({
      status: 0,
      offset: reqInfo.offset,
      total: result.length,
      message: "获取成功",
      data: result,
    });
  });
};
exports.getByTermId = (req, res) => {
  const termId = req.query.term_id;
  if (!termId) {
    return res.send({
      status: 1,
      message: "终端id缺失",
    });
  }
  let sql_query = "select * from terminal_list where id = ?";
  db.query(sql_query, termId, (err, result) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    if (result.length == 0) {
      return res.send({
        status: 1,
        message: "查找的终端不存在",
      });
    }
    res.send({
      status: 0,
      message: "查找成功",
      data: result,
    });
  });
};
